import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/data/NoticeEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:mtf/app/utils/AppUtils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class noticeListController extends BaseController{
  RefreshController refreshController = RefreshController(
      initialRefresh: true);
  var page = 1;
  RxList<ListElement> noticeList = <ListElement>[].obs;

  void refresh() {
    page=1;
    reqNotice();
  }

  void load() {
    page+=1;
    reqNotice();
  }

  @override
  void onInit() {
    super.onInit();
    reqNotice();
  }

  void reqNotice() async {
    var resp=await HttpManager.reqNoticeList(page);
        if(resp.isSuccess()&&resp.data!=null){
          if(page==1){
            noticeList.assignAll(resp.data!.list);
            refreshController.refreshCompleted();
          }else{
            noticeList.addAll(resp.data!.list);
            if(resp.data!.list.isEmpty){
              refreshController.loadNoData();
            }else{
              refreshController.loadComplete();
            }}
        }else{
          refreshController.refreshCompleted();
          refreshController.loadComplete();
        }

      }


  void clickNoticeDetail(ListElement listElement){
    Get.toNamed(Routes.NOTICE_DETAIL,arguments: {
      "title": listElement.title,
      "time": AppUtils.dateToString(listElement.updatedAt),
      "content": listElement.content
    });
  }

}
